package com.inspur.incubator.common.interceptor;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.codehaus.jackson.map.ser.std.StdArraySerializers.FloatArraySerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor {

	private List<String> excludedUrls;
	

	
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {

	}

	
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {

	}

	
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		
		
		System.out.println("---------------------------------进入拦截器");
		// 获取请求的URL
		String url = request.getRequestURI();
		System.out.println(url);
		String icon = request.getParameter("icon");
		// 跳过不需要判断的URL
		for (String exurl : excludedUrls) {
			if (url.contains(exurl)) {
				return true;
			}
		}
		// 获取Session
		HttpSession session = request.getSession();
		String ddd=(String) session.getAttribute("islogin");
		String roleStr=(String)session.getAttribute("roleList");
		if(ddd!=null&&ddd.equals("true")){
			if (roleStr!=null&&roleStr.indexOf(url)!=-1) {
				return true;
			}else{
				response.sendRedirect(request.getContextPath() + "/admin/login");
				return false;
			}
		}else{
			// 不符合条件的，跳转到登录界面
			response.sendRedirect(request.getContextPath() + "/admin/login");
			
			return false;
		}
		
	}

	public void setExcludedUrls(List<String> excludedUrls) {
		this.excludedUrls = excludedUrls;
	}

}
